import Vue from 'vue'
import VueRouter from 'vue-router'
import loginHou from '../views/loginHou.vue'
import HomeView from '../views/HomeView.vue'

Vue.use(VueRouter)

const routes = [
  {
    path: '/component',
    component: () => import('../views/Component.vue'),
    redirect:'/component/user',//重定向,直接访问/component将会跳转到该路径
    children:[
      {
        path: 'user',
        component: () => import('../views/User.vue'),
      },
      {
        path: 'views',
        component: () => import('../views/Views.vue'),
      },
    
      {
        path: 'list',
        component: () => import('../views/List.vue'),
      },
      {
        path: 'hotword',
        component: () => import('../views/HotWord.vue'),
      },
    ]
  },
  {
    path: '/user',
    component: () => import('../views/User.vue'),
  },
  {
    path: '/list',
    component: () => import('../views/List.vue'),
  },
  {
    path: '/',
    redirect:'/loginhou',//重定向,直接访问/component将会跳转到该路径
    component: HomeView
  },

  {
     path: '/loginhou',
     name: 'loginhou',
     component: () => import('../views/loginHou.vue'),
    },
  

]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

// 设定路由导航守卫,当用户没有登录时,不允许跳转其他页面
router.beforeEach((to, from, next) => {
  // 如果访问的是登录界面则直接放行
  if (to.path === '/loginhou' ) return next()
  //获取用户页面token信息
  let token = window.sessionStorage.getItem('token')
  console.log(token)
  //如果token数据为null则跳转到指定路径
  if (!token) return next("/loginhou")
  next()
})
export default router
